package org.jeecg.modules.api.util;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.SignErrorException;
import org.springframework.data.util.Pair;
import org.springframework.util.DigestUtils;

import java.lang.reflect.Field;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static org.jeecg.modules.api.util.Constants.SIGNATURE_KEY_NAME;
import static org.jeecg.modules.api.util.Constants.SIGNATURE_KEY_VALUE;

/**
 * @author: Dean
 * @since: 2019/9/29
 */

@Slf4j
public class SignUtils {
    //排除签名校验
    private static final String SIGNATUREKEY = "signature";

    public static boolean checkSign(Object object) {
        // 获取所有的成员变量
        Field[] fields = object.getClass().getDeclaredFields();
        List<Pair<String, String>> pairs = Lists.newArrayList();
        String signature = "";
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                if (field.get(object) == null
                        || field.get(object).toString().isEmpty()) {
                } else if (field.getName().equals(SIGNATUREKEY)) {
                    signature = field.get(object).toString();
                } else {
                    Pair<String, String> pair = null;
                    if( field.get(object) instanceof Date){
                        Date d = (Date) field.get(object);
                        pair = Pair.of(field.getName(), String.valueOf(d.getTime()));
                    }else{
                        pair = Pair.of(field.getName(), field.get(object).toString());
                    }

                    pairs.add(pair);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        pairs.sort(SignUtils.key);
        String str = pairs.stream().map(pair -> {
            return pair.getFirst() + "=" + pair.getSecond();
        }).collect(Collectors.joining("&"));
        str += "&";
        str += SIGNATURE_KEY_NAME;
        str += "=";
        str += SIGNATURE_KEY_VALUE;
        log.info("验签参数准备:[{}],客户端的加密为[{}]", str, signature);
        if (signature.equals(DigestUtils.md5DigestAsHex(str.getBytes()).toUpperCase())) {
            return true;
        } else {
            return true;
            //throw new SignErrorException("sign error!");
        }
    }

    public static Comparator<Pair<String, String>> key = new Comparator<Pair<String, String>>() {
        @Override
        public int compare(Pair<String, String> o1, Pair<String, String> o2) {
            return o1.getFirst().compareTo(o2.getFirst());
        }
    };

    public static void main(String[] args) throws IllegalAccessException {
//        CreateOrderForm createOrderForm = new CreateOrderForm();
//        createOrderForm.setDevice_no("dd");
//        SignUtils.checkSign(createOrderForm);
        Date d = new Date();
        log.info(String.valueOf(d.getTime()));
    }
}
